<?php

namespace backend\models;

use Yii;

/**
 * This is the model class for table "baserolemaster".
 *
 * @property int $MasterId
 * @property int $RoleId
 */
class Baserolemaster extends \yii\db\ActiveRecord
{
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'baserolemaster';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['MasterId', 'RoleId'], 'required'],
            [['MasterId', 'RoleId'], 'integer'],
            [['MasterId', 'RoleId'], 'unique', 'targetAttribute' => ['MasterId', 'RoleId']],
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'MasterId' => 'Master ID',
            'RoleId' => 'Role ID',
        ];
    }


    /**
     * @param $agent
     * @return array
     * @throws \yii\db\Exception
     * 代理身份下团队结构
     */
    public function getAgentLevelOrgChart($agent)
    {
        $uid = $agent->AgentId;
        $sql = "SELECT a.PlayerId,a.CollectDate,a.Level,b.PlayerName,a.ParentId 
                FROM baseagency a 
                inner join Player_tbl b on a.PlayerId = b.PlayerID 
                WHERE FIND_IN_SET(CONVERT('{$uid}',char(11)),FullDir) 
                order by a.Level desc 
                ";
        $data = yii::$app->db->createCommand($sql)->queryAll();
        foreach ($data as $key => $v){
            $agency = Baseagency::findOne(['AgentId'=>$v['ParentId']]);
            $master = Basemaster::findOne(['UserName'=>'DL'.$v['PlayerId']]);
            if (empty($master->AreaType)){
                $userlv = (empty($v['Level']) ? '总代理 ' : $v['Level'].'级代理 ');
            }else{
                $province = Baseareas::findOne(['ID'=>$master->Province]);//省份
                $city = Baseareas::findOne(['ID'=>$master->City]);
                if ($master->AreaType ==1){
                    $userlv = $province->ShortName.$city->ShortName.'代理';
                }else{
                    $userlv = $province->ShortName.$city->ShortName.(empty($v['Level']) ? '总代理 ' : $v['Level'].'级代理 ');
                }
            }
            $data[$key]['Id'] = intval($v['PlayerId']);
            $data[$key]['parentId'] = empty($agency) ? 0 :intval($agency->PlayerId);
            $data[$key]['userid'] = intval($v['PlayerId']);
            $data[$key]['name'] = $v['PlayerName'];
            $data[$key]['userlv'] = $userlv;
            $data[$key]['date'] = date('Y-m-d', strtotime($v['CollectDate']));
            $data[$key]['image'] = '/img/faces/vip_'.$v['Level'].'.png';
            unset($data[$key]['ParentId']);
            unset($data[$key]['Level']);
            unset($data[$key]['PlayerName']);
            unset($data[$key]['CollectDate']);
            unset($data[$key]['PlayerId']);
        }
        return $data;
    }



    /**
     * @param $agent
     * @return array
     * @throws \yii\db\Exception
     * 管理员身份下代理结构
     */
    public function getAllAgentOrgChart()
    {
        $sql = "SELECT a.PlayerId,a.CollectDate,a.Level,b.PlayerName,a.ParentId,a.AdminId  
                FROM baseagency a 
                inner join Player_tbl b on a.PlayerId = b.PlayerID 
                inner join basemaster m on m.MasterId = a.AdminId 
                where m.AreaType <> 1 
                order by a.Level desc 
                ";
        $data = yii::$app->db->createCommand($sql)->queryAll();
        foreach ($data as $key => $v){
            $agency = Baseagency::findOne(['AgentId'=>$v['ParentId']]);
            $data[$key]['Id'] = intval($v['PlayerId']);
            $data[$key]['parentId'] = empty($agency) ? 0 :intval($agency->PlayerId);
            $data[$key]['userid'] = intval($v['PlayerId']);
            $data[$key]['name'] = $v['PlayerName'];
            $data[$key]['userlv'] = (empty($v['Level']) ? '总代理 ' : $v['Level'].'级代理 ');
            $data[$key]['date'] = date('Y-m-d', strtotime($v['CollectDate']));
            $data[$key]['image'] = '/img/faces/vip_'.$v['Level'].'.png';
            unset($data[$key]['ParentId']);
            unset($data[$key]['Level']);
            unset($data[$key]['PlayerName']);
            unset($data[$key]['CollectDate']);
            unset($data[$key]['PlayerId']);
            unset($data[$key]['AdminId']);
        }
        return $data;
    }


}
